/*
 * public.js
 * name:xiaojia
 * email:iatt@qq.com
 * qq:273142650
 * time:2011.9.18 10:17
*/

	layer = {
	
		alert: function (json) {
			
			top.$.jLayer.alert(json);
		
		},
		
		confirm: function (json) {
			
			top.$.jLayer.confirm(json);
			
		},
		
		layer: function (config, url, callback) {
			
			if (typeof config.id == 'undefined') {
				
				config['id'] = Math.random().toString().replace(/^0\./ig, '');				
				
			}
			
			config['notWebos'] = true;
		
			top.jPushApplication.getLayer(config, url);
			
		},
		
		del: function (callback, subject) {
		
			if (typeof subject == 'undefined') {
				
				subject = '确认删除吗';
				
			}
		
			top.$.jLayer.confirm({
			
				subject: subject,
				
				sure: function () {
				
					callback();
				
				}
				
			});
		
		}
		
	}
	
	getWindow = function (windowID) {
		
		return top[windowID];
		
	}
	
	//加载页
	toPage = function (url, type) {
		now_src = $(parent.document.getElementById('model_id')).attr('src').replace(/(\s|\S)*\?/ig,'').split('&');
		$mod = get('mod', 'all', $(parent.document.getElementById('model_id')).attr('src'));
		if ($mod != url || type == 0) {
			$(parent.document.getElementById('model_id')).attr('src','./interface.php?' + url + '&sid='+get('sid')+'&rand=' + Math.random());
		}
	}
	
	toMenu = function (m1, m2) {
		par = $(window.parent.frames['left_id'].document);
		par.find('#menu').find('.menu:eq('+m1+')').find('div').click();
		if (typeof(m2) != 'undefined') {
			par.find('#menu').find('.menu:eq('+m1+')').find('ul:eq(0)').find('li:eq('+m2+')').click();
		}
	}
	
	loginOut = function () {
		fn = function () {
			toPage('mod=member/loginout.php&type=server');
		}
		par = window.parent.frames['model_id'];
		par.Jlayer().confirm({title:'警告', content:'确认退出吗？',opacity:0.35,callback:fn});
	}
	
	toTableMenu = function (menu, id) {
		$('#'+id).find('.list_title_m').find('li:eq('+menu+')').find('a').click();
	}
	
	get = function (name, type, url) {
		if (typeof(url) == 'undefined') {
			url = location.href;
		}
		url = url.replace(/(\s|\S)[^\?]*\?/ig, '').split('&');
		for (i = 0; i < url.length; i++) {
			$left = url[i].replace(/[=](\s|\S)*/ig, '');
			if ($left == name) {
				if (type == 'all') {
					return url[i];
				} else {
					return url[i].replace(/(\s|\S)*[^=]*=/ig, '');
				}
			}
		}
		return '';
	}
	
	checkbox_all = function (_this) {
		if (_this.checked) {
			$(document.body).find('input:[name='+$(_this).attr('name')+']').attr('checked', true);
		} else {
			$(document.body).find('input:[name='+$(_this).attr('name')+']').attr('checked', false);
		}		
	}
	
	table_delete = function (url, $ref, $div, $this) {
		$remove = $($this).parent('div').parent('td').parent('tr');
		$loading = $($this).parent();
		$.ajax({
			type: 'POST',
			url: 'interface.php?mod=' + url + '&type=ajax',
			cache: false,
			beforeSend: function () {
				$($this).parent().html('<img src="style/images/icons/loading_1.gif" style="display:none;"/>');
				$loading.find('img').fadeIn(300);
			},
			success: function (xmlHttp) {
				ajax_loadPage($ref, $div);
			},
			error: function (xmlHttp) {
				alert(xmlHttp);
			}
		});
	}
	
	my_disk = function (act, iframe) {
		
		if (iframe == 1) {
			iframeID = document.getElementsByTagName('iframe')[ document.getElementsByTagName('iframe').length - 1 ].id;
		} else {
			iframeID = '';
		}
		
		Jlayer().layer({title:'我的文件夹', width:756, height:500, only:'true', id: 'my_disk', iframe:{src:'interface.php?mod=member.php&iframeid='+iframeID+'&action=dir&act='+act+''}});
		
	}
	
	table_list_menu = function (url, div, th, callback) {
		$(th).parent().parent().find('li').attr('class','');
		$(th).parent().attr('class','click');
		ajax_loadPage(url, div, '', callback);
	}
	
	float_tip = function (obj,con) {
		$(obj).css('position','relative');
		objW = $(obj).width();
		objH = $(obj).height() + 5;
		div = document.createElement('div');
		div.style.cssText = ';height:20px;position:absolute;top:-'+objH+'px;left:0;';
		div.innerHTML = '<div style="height:20px;line-height:20px;float:left;border:solid 1px #ff6600;background:#ffcdab;padding:0 5px;text-align:center;">'+con+'</div>';
		obj.appendChild(div);
		div.style.left = '-' + ($(div).width() / 2 - objW / 2 ) + 'px';
		obj.onmouseout = function () {
			obj.removeChild(div);
		}
	}
	
	//到内容页
	into = function (href, id) {
		ajax_loadPage(href, $('#'+id).find('.list_con'));
	}
	
	editOption = function () {
		var options = {
			allowFileManager : true,
			allowMediaUpload : false,
			allowFlashUpload : false,
			allowImageUpload : false,
			resizeType : 1,
			items : 
				[
					'source', '|', 'undo', 'redo', '|', 'preview', 'print', 'template', 'cut', 'copy', 'paste',
					'plainpaste', 'wordpaste', '|', 'justifyleft', 'justifycenter', 'justifyright',
					'justifyfull', 'insertorderedlist', 'insertunorderedlist', 'indent', 'outdent', 'subscript',
					'superscript', 'clearhtml', 'quickformat', 'selectall', '|', 'fullscreen', '/',
					'formatblock', 'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold',
					'italic', 'underline', 'strikethrough', 'lineheight', 'removeformat', '|', 'image',
					'table', 'hr', 'emoticons', 'map', 'code', 'pagebreak',
					'link', 'unlink', 'anchor','|'
				]
		};
		return options;
	}
	
	//加载页
	ajax_loadPage = function (url, div, data, callback) {
		$.ajax({
			type: 'POST',
			url: 'interface.php?mod=' + url + '&type=ajax&sid='+get('sid'),
			data: data,
			cache: false,
			dataType: 'html',
			beforeSend: function () {
				$(div).html('<div style="text-align:center;width:100%;display:none;line-height:150px;">正在加载</div>');
				$(div).find('div').fadeIn(300);
			},
			success: function (xmlHttp) {
				$(div).html(xmlHttp);
				typeof(callback) != 'undefined' ? callback() : '';
				model_main();
			},
			error: function (xmlHttp) {
				$(div).html(xmlHttp);
			}
		});
	}
	
	paging_search = function (obj, file) {
		if ($(obj).val() != '') {
			$(obj).parent().parent().css('background-image','none');
			$(obj).parent().find('button').fadeIn();
		} else {
			$(obj).parent().parent().css('background-image', file);
			$(obj).parent().find('button').fadeOut();
		}
	}
	
	check_input = function (str, type) {
	
		emailRegular = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
	
		switch (type) {
			case 'mobilephone':
	
				tmp = /^1[3-9]\d{9}$/;
				if (tmp.test(str)) {
					
					return true;
					
				} else {
					
					return false;
					
				}
				
			break;
			case 'telephone':
			
				if (str.search(/\-/ig) > 0) {
					
					if (str.replace(/\d{4}\-\d{6,}/ig, '') != '') {
						
						return false;
						
					} else {
					
						return true;
					
					}
					
				} else {
					
					if (str.replace(/\d{6,}/ig, '') != '') {
						
						return false;
						
					} else {
					
						return true;
					
					}
					
				}
				
			break;
			case 'email':
			
				if (str.replace(emailRegular, '') != '') {
					
					return false;
				
				} else {
				
					return true;
				
				}
			
			break;
			case 'qq':
			
				if (str.search(/\@/ig) > 0) {
					
					if (str.replace(emailRegular, '') != '') {
						
						return false;
						
					} else {
					
						return true;
						
					}
					
				} else {
				
					if (str.replace(/\d{5,12}/ig, '') != '') {
						
						return false;
						
					} else {
					
						return true;
					
					}
				
				}
			
			break;
		}
		
	}
	
	user_info = function (id) {
	
		top.frames['model_id'].Jlayer().layer({
			title: '用户信息',
			width: 600,
			height: 500,
			top: 50,
			iframe: {
				src: 'interface.php?mod=userinfo.php&action=view_userinfo&id='+id
			}
		});
	
	}
	
	Copy = function (id) {
		
		if(typeof clipboardData != 'undefined') {
		
			clipboardData.setData('Text',document.getElementById(id).value);
			Jlayer().alert({content:'复制成功'});
		
		} else {
		
			document.getElementById(id).select();
			Jlayer().alert({content:'您的浏览器不支持复制操作，请选中手动复制'});
		
		}
		
	}
	
	password_level = function (obj) {
		
		$(obj).keyup(
			function () {
				val = $(this).val().trim();
				
				level = $('.password-level div');
				
				if (val.search(/[A-z0-9\W]/ig) >= 0) {
					level.css('background', '#fff');
					level.slice(0,2).css('background', '#f00');
				}
				
				if ((val.search(/[A-z]/ig) >= 0 && val.search(/[0-9]/ig) >= 0) || (val.search(/[0-9]/ig) >= 0 && val.search(/[\W]/ig) >= 0) || (val.search(/[A-z]/ig) >= 0 && val.search(/[\W]/ig) >= 0)) {
					level.css('background', '#fff');
					level.slice(0,4).css('background', '#F60');
				}
				
				if (val.search(/[A-z]/ig) >= 0 && val.search(/[0-9]/ig) >= 0 && val.search(/[\W]/ig) >= 0) {
					level.css('background', '#fff');
					level.slice(0,6).css('background', '#5ba300');
				}
				
				if (val == '') {
					level.css('background', '#fff');
				}
			}
		);
		
	}
	
	offSet=function(obj) {
		divTop = obj.offsetTop;
		divLeft = obj.offsetLeft;
		while( obj = obj.offsetParent ) {
			divTop += obj.offsetTop;
			divLeft += obj.offsetLeft;
		}
		return divTop + ',' + divLeft;
	}